package com.vfa.file;

import java.io.File;
import java.util.ArrayList;

import com.vfa.vocaburay.R;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;

public class DialogFolder implements OnItemClickListener,OnItemLongClickListener,OnClickListener {

	private ListView mListFile;
	private Button mBtnBack;
	private LinearLayout mLinearContainer;
	private Context mContext;
	private FileAdapter mFileAdapter;
	private ListArrayAdapterDialog mAdapter;
	private AlertDialog mDialog;	
	private DialogCloseListener mListener;
	private String mCurPath;
	private String mTitle;
	
	private ArrayList<String> mPrePath = new ArrayList<String>();
	

	public DialogFolder(Context context) {
		this.mContext = context;
	}
	public DialogFolder(Context context,String title) {
		this.mContext = context;
		mTitle = title;
	}
	
	public DialogFolder(Context context,DialogCloseListener listener) {
		this.mContext = context;
		this.mListener = listener;	
	}

	public void formatContainer() {
		mPrePath.clear();
		mPrePath.add("/");
		mBtnBack = new Button(mContext);
		mBtnBack.setText("Back");
		//mBtnBack.setCompoundDrawables(mContext.getResources().getDrawable(R.drawable.icon_btndialog_back), null, null, null);
//		mBtnBack.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		mBtnBack.setOnClickListener(this);
		
		this.mListFile = new ListView(mContext);
		this.mFileAdapter = new FileAdapter(mContext,1);
		this.mAdapter = new ListArrayAdapterDialog(mContext, R.layout.list_items,
				mFileAdapter.getFileInfos());
		mListFile.setAdapter(mAdapter);
//		mListFile.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		mListFile.setOnItemClickListener(this);
		mListFile.setOnItemLongClickListener(this);
		

		mLinearContainer = new LinearLayout(mContext);
		mLinearContainer.setLayoutParams(new LayoutParams(
				LayoutParams.FILL_PARENT, 200));
		mLinearContainer.setOrientation(LinearLayout.VERTICAL);

		mLinearContainer.addView(mBtnBack);
		mLinearContainer.addView(mListFile);
		

	}

	public void setTitle(String title) {
		mTitle = title;
	}

	public void showDialog() {
		formatContainer();

		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		builder.setTitle(mTitle);
		builder.setView(mLinearContainer);
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				for(FileInfo file : mFileAdapter.getFileInfos()){
					if(file.isChecked){
						mCurPath = file.mFilePath;
						mListener.onClose();
						break;
					}
				}
			}
		});
		builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
			}
		});
		mDialog = builder.create();
		mDialog.show();
	}
	
	public String getCurPath(){
		return mCurPath;
	}
	

	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		File file = new File(mFileAdapter.getFileInfos().get(position).mFilePath);

		if (file.isDirectory() && file.canRead()) {
			mPrePath.add(mFileAdapter.getFileInfos().get(position).mFilePath);
			
			mFileAdapter.getDirSearch(file.getPath());
			mAdapter.reset();
			mAdapter.notifyDataSetChanged();
		}
	}
	
	
	public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
		mCurPath = mFileAdapter.getFileInfos().get(position).mFilePath;
		mListener.onClose();
		mDialog.cancel();
		return true;
	}
	
	@Override
	public void onClick(View v) {
		if(mPrePath.size()>1){
			mPrePath.remove(mPrePath.size()-1);
			mAdapter.reset();
			mFileAdapter.getDirSearch(mPrePath.get(mPrePath.size()-1));
			mAdapter.notifyDataSetChanged();
		}		
	}
	
	public interface DialogCloseListener{
		public void onClose();
	}
}
